//
//  XLECGView.m
//  xinLing
//
//  Created by 于鹏 on 2017/7/8.
//  Copyright © 2017年 YIVIEW. All rights reserved.
//

#import "XLECGView.h"
//#import <Masonry.h>
//#import <XLBaseModule/XLAppMacro.h>
#import "Masonry.h"
#import "XLAppMacro.h"

#define  Offset 10
#define RefreshViewHeight 200
#define RightMargin  60
#define PlaceHolderText @"加载中..."
#define TextColor [UIColor whiteColor]
//#define RefreshViewHeight KScreenHeight-2*Offset-64
//static const NSInteger kMaxContainerCapacity = 750;

@interface XLECGView ()

@property (nonatomic, strong)NSTimer *ecgTimer;

@property (nonatomic , assign) NSInteger numberOfRefreshElements;
@property (nonatomic , assign) CGPoint *refreshPointContainer;
//@property (nonatomic , assign) NSInteger numberOfRefresh2Elements;
//@property (nonatomic , assign) CGPoint *refreshPoint2Container;

@end

@implementation XLECGView
{
     NSInteger currentPointsCount;
     NSInteger dataSourceCounterIndex;
     CGFloat   xCoordinateInMoniter;
//     NSInteger workIndex;
    NSInteger _timerTag;


}
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        currentPointsCount = 0;
        dataSourceCounterIndex = -1;
        xCoordinateInMoniter = 0;
        _timerTag = 0;
        
        self.curveStrokeColor = CurveStrokeColorOfGreen;
        [self addSubview:self.refreshMoniterView];
        [self addSubview:self.des1Label];
        [self addSubview:self.des2Label];
        
        [self.refreshMoniterView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_equalTo(Offset);
            make.right.equalTo(self.mas_right).offset(-RightMargin);
//            make.height.mas_equalTo(RefreshViewHeight);
            make.bottom.equalTo(self.mas_bottom).offset(0);
        }];

        [self.des1Label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.refreshMoniterView.mas_right).offset(5);
//            make.centerY.equalTo(self.mas_centerY).offset(0);
            make.top.mas_equalTo(Offset);
        }];
        [self.des2Label mas_makeConstraints:^(MASConstraintMaker *make) {
           make.left.equalTo(self.refreshMoniterView.mas_right).offset(5);
           make.top.equalTo(self.des1Label.mas_bottom).offset(Offset);
        }];
    }
    return self;
}



#pragma mark - Getter
- (HeartLive *)refreshMoniterView
{
    if (!_refreshMoniterView) {
        _refreshMoniterView = [[HeartLive alloc]init];
        _refreshMoniterView.backgroundColor = [UIColor blackColor];
        _refreshMoniterView.strokeColor = self.curveStrokeColor;
    }
    return _refreshMoniterView;
}

- (UILabel *)des1Label {
    if (!_des1Label) {
        _des1Label  =[[UILabel alloc]init];
        _des1Label.font = XLFONT(15);
        _des1Label.text = PlaceHolderText;
        _des1Label.textColor = TextColor;
        _des1Label.numberOfLines = 0;
    }
    return _des1Label;
}
- (UILabel *)des2Label {
    if (!_des2Label) {
        _des2Label  =[[UILabel alloc]init];
        _des2Label.font = XLFONT(15);
        _des2Label.text = PlaceHolderText;
        _des2Label.textColor = TextColor;
        _des2Label.numberOfLines = 0;
    }
    return _des2Label;
}
#pragma mark - Setter
- (void)setCurveStrokeColor:(UIColor *)curveStrokeColor {
    _curveStrokeColor = curveStrokeColor;
    self.refreshMoniterView.strokeColor = curveStrokeColor;
}
- (void)setMaxContainerCapacity:(NSInteger)maxContainerCapacity {
    _maxContainerCapacity = maxContainerCapacity;
    self.refreshPointContainer = malloc(sizeof(CGPoint) * maxContainerCapacity);
    memset(self.refreshPointContainer, 0, sizeof(CGPoint) * maxContainerCapacity);
}
#pragma mark - Public Method
- (void)invalidTimer {
    if (self.ecgTimer.isValid) {
        [self.ecgTimer invalidate];
        self.ecgTimer = nil;
    }
}

- (void)createWorkDataSourceWithTimeInterval:(NSTimeInterval )timeInterval{
    if (_timerTag < 1) {
        _timerTag++;
        self.ecgTimer =  [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(timerRefreshFun) userInfo:nil repeats:YES];
    }
}

#pragma mark - Private Method
//刷新方式绘制
- (void)timerRefreshFun {
    [self addPointAsRefreshChangeform:[self bubbleRefreshPoint]];
    [self.refreshMoniterView fireDrawingWithPoints:self.refreshPointContainer pointsCount:self.numberOfRefreshElements];
}
- (void)addPointAsRefreshChangeform:(CGPoint)point
{
    if (currentPointsCount < self.maxContainerCapacity) {
        self.numberOfRefreshElements = currentPointsCount + 1;
        self.refreshPointContainer[currentPointsCount] = point;
        currentPointsCount ++;
    } else {
        NSInteger workIndex = 0;
        while (workIndex != self.maxContainerCapacity - 1) {
            self.refreshPointContainer[workIndex] = self.refreshPointContainer[workIndex + 1];
            workIndex ++;
        }
        self.refreshPointContainer[self.maxContainerCapacity - 1] = point;
        self.numberOfRefreshElements = self.maxContainerCapacity;
    }
    
}

- (CGPoint)bubbleRefreshPoint{
    if (self.ecgDataSource.count == 0) {
        return CGPointZero;
    }
    dataSourceCounterIndex ++;
    
    if (dataSourceCounterIndex == self.maxContainerCapacity) {
        xCoordinateInMoniter = 0;
    }
    dataSourceCounterIndex %= [self.ecgDataSource count];
    
    //    CGFloat pixelPerPoint = [NSString stringWithFormat:@"%.1f",CGRectGetWidth(self.refreshMoniterView.frame)/self.ecgDataSource.count].floatValue;
    CGFloat pixelPerPoint = [NSString stringWithFormat:@"%.1f",CGRectGetWidth(self.refreshMoniterView.frame)/self.maxContainerCapacity].floatValue;
    
    CGFloat pointY = [self.ecgDataSource[dataSourceCounterIndex] floatValue];
    CGFloat targetY = pointY/2;
    float f = fabs(targetY);
    
    CGPoint targetPointToAdd = (CGPoint){xCoordinateInMoniter,f};
    
    xCoordinateInMoniter += pixelPerPoint;
    return targetPointToAdd;
}

- (void)dealloc {
    [self invalidTimer];
    
}
@end
